﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.DataContract.Result;
using ThresholdAssistant.Enums;
using ThresholdAssistant.WebAPI.Models;
using ThresholdAssistant.WebAPI.Repository;

namespace ThresholdAssistant.WebAPI.Service
{
    /// <summary>
    /// 数组服务
    /// </summary>
    public class SiteService : BaseService, IBaseAutofac
    {
        /// <summary>
        /// 数组仓储
        /// </summary>
        public SiteRepository SiteRepository { get; set; }

        /// <summary>
        /// 数据仓储
        /// </summary>
        public RecordRepository DataRepository { get; set; }

        /// <summary>
        /// 数据明细仓储
        /// </summary>
        public RecordRepository RecordRepository { get; set; }
        /// <summary>
        /// 获取数组列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ContractResult<IEnumerable<SiteData>>> GetListAsync(GetSiteListParam param)
        {
            var result = new ContractResult<IEnumerable<SiteData>>();
            var SiteList = await SiteRepository.GetListAsync(param.GroupId);
            result.Data = SiteList?.Select(x => new SiteData
            {
                GroupId = x.GroupId,
                Id = x.Id,
                Name = x.Name
            })?.ToList();
            return result.SetSuceccful(result.Data);
        }

        /// <summary>
        /// 新增标签
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ContractResult<long>> AddAsync(AddSiteParam param)
        {
            //todo 判断标签类型是否存在
            var result = new ContractResult<long>();

            var SiteList = await SiteRepository.GetListAsync(param.GroupId);
            if(SiteList != null && SiteList.Count() >= 5)
            {
                result.SetError(ErrorTypeEnum.SiteOutsideMaxCount);
                return result;
            }                

            var id = await SiteRepository.AddAsync(new Site
            {
                GroupId = param.GroupId,
                Name = param.Name,
                CreateId = param.MemberId,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                Remark = ""
            });
            result.Data = id;
            return result.SetSuceccful();
        }

        /// <summary>
        /// 获取标签信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ContractResult<SiteData>> GetInfoAsync(GetSiteInfoParam param)
        {
            var result = new ContractResult<SiteData>();
            var siteInfo = await SiteRepository.GetInfoAsync(param.Id);
            result.Data = new SiteData
            {
                GroupId = siteInfo?.GroupId ?? 0,
                Id = siteInfo?.Id ?? 0,
                Name = siteInfo?.Name ?? "",
            };
            return result.SetSuceccful();
        }

        /// <summary>
        /// 删除标签下的数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ContractResult> DeleteAsync(DeleteSiteParam param)
        {
            var result = new ContractResult();
            var siteIdList = new List<long> { param.Id };
            await SiteRepository.DeleteAsync(siteIdList);

            // 删除数据明细
            await RecordRepository.DeletesBySiteIdAsync(siteIdList);

            return result.SetSuceccful();
        }

        /// <summary>
        /// 修改标签信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ContractResult> UpdateAsync(UpdateSiteParam param)
        {
            var result = new ContractResult();
            var SiteInfo = await SiteRepository.GetInfoAsync(param.Id);
            if (SiteInfo != null)
            {
                SiteInfo.Name = param.Name;
                SiteInfo.UpdateTime = DateTime.Now;
                await SiteRepository.UpdateAsync(SiteInfo);
            }
            return result.SetSuceccful();
        }
    }
}
